package com.info33.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.exception.TransactionalException;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dao.SysAppMenuFastDao;
import com.info33.platform.system.entity.SysAppMenuFast;
import com.info33.platform.system.service.SysAppMenuFastService;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class SysAppMenuFastServiceImpl extends ServiceImpl<SysAppMenuFastDao, SysAppMenuFast> implements SysAppMenuFastService {

    @Autowired
    private SysAppMenuFastDao sysAppMenuFastDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Object> appChange(List<SysAppMenuFast> dtoList, SysUserVO appCurrentUser) {
        // 删除所有旧菜单
        List<SysAppMenuFast> list = this.list(new QueryWrapper<SysAppMenuFast>()
                .select(TableConstant.ID)
                .eq(TableConstant.CREATE_BY, appCurrentUser.getSysTenantUserVO().getId()));
        if (!ObjectUtils.isEmpty(list) && !this.removeByIds(list.stream().map(SysAppMenuFast::getId).collect(Collectors.toList()))) {
            throw new TransactionalException();
        }
        // 保存新数据
        if (Boolean.FALSE.equals(this.saveBatch(dtoList))) {
            throw new TransactionalException();
        }
        return Result.ok();
    }
}